package com.jerei.store.service.impl;

import com.jerei.store.entity.Product;
import com.jerei.store.entity.TableCart;
import com.jerei.store.entity.TableCartProduct;
import com.jerei.store.mapper.ProductMapper;
import com.jerei.store.mapper.TableCartMapper;
import com.jerei.store.service.ITableCartService;
import com.jerei.store.service.ex.DeleteException;
import com.jerei.store.service.ex.InsertException;
import com.jerei.store.service.ex.SelectException;
import com.jerei.store.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TableCartServiceImpl implements ITableCartService {
    @Autowired
    private TableCartMapper mapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addTC(Integer uid, String username,Integer pid,Integer num) {
        //输出日志
        System.out.println("uid="+uid+",pid="+pid);

        //判断是否有相同商品加入购物车
        TableCart tableCart=mapper.selectByPid(pid,uid);
        if(tableCart!=null){
            System.out.println("测试相同产品");
            //如果不等于null
            Integer productNum=tableCart.getNum();
            Integer rows= mapper.updateByPid(productNum+num,pid,uid,username,new Date());

            System.out.println("rows="+rows);
            if(rows!=1){
                throw new UpdateException("修改数据异常！请重新尝试");
            }
            return;
        }

        TableCart cart=new TableCart();
        Product product= productMapper.findById(pid);
        cart.setPrice(product.getPrice());
        cart.setNum(num);
        cart.setUid(uid);
        cart.setPid(pid);
        cart.setCreatedUser(username);
        cart.setModifiedUser(username);
        Date date=new Date();
        cart.setCreatedTime(date);
        cart.setModifiedTime(date);
        Integer rows=mapper.insertTC(cart);
        if(rows!=1){
            throw new InsertException("加入购物车异常，请重新操作");
        }
    }

    @Override
    public List<TableCartProduct> findAllByUid(Integer uid) {
        System.err.println("购物车加载  uid="+uid);

        List<TableCartProduct> list=mapper.selectByUid(uid);

        return list;
    }

    @Override
    public void delAllByCid(Integer[] arr) {

        if(arr.length==0){
            throw new DeleteException("没有勾选！");
        }

        Integer rows=mapper.delByCid(arr);
        if(rows<1){
            throw new DeleteException("删除失败，请重新尝试");
        }

    }

    @Override
    public void changeNum( Integer val, Integer cid, String username) {
        System.err.println("val="+val);
        TableCart tableCart=mapper.selectByCid(cid);
        Integer cartNum=tableCart.getNum();

        if(val==1){
            cartNum+=1;

        }else if (val==0){
            if(cartNum==0){
                throw new UpdateException("已经是0了，不能再减少了！");
            }
            cartNum-=1;
        }else {
            throw new UpdateException("添加异常，发生未知错误");
        }
        TableCart cart=new TableCart();
        cart.setNum(cartNum);
        cart.setPrice(tableCart.getPrice());
        cart.setCid(cid);
        cart.setModifiedTime(new Date());
        cart.setModifiedUser(username);
        Integer rows=mapper.updateNumByCid(cart);
        if(rows!=1){
            throw new UpdateException("修改数据异常");
        }



    }

    @Override
    public List<TableCartProduct> findAllByCid(Integer[] arr) {
        if(arr.length==0){
            throw  new SelectException("勾选的数量为0,请重新选择！");
        }
        List<TableCartProduct> list=mapper.findAllByCid(arr);
        if(list.isEmpty()){
            throw new SelectException("未知错误,查询异常");
        }

        return list;
    }


}
